package com.hertz.appr.test;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.swing.*;
import javax.imageio.*;

import org.apache.sanselan.ImageFormat;
import org.apache.sanselan.Sanselan;
import org.apache.sanselan.SanselanConstants;
import org.apache.sanselan.formats.jpeg.JpegConstants;
import org.apache.sanselan.formats.png.PngConstants;
import org.apache.sanselan.formats.tiff.constants.TiffConstants;
import org.springframework.web.util.HtmlUtils;
import org.xhtmlrenderer.simple.Graphics2DRenderer;
import org.xhtmlrenderer.swing.Java2DRenderer;
import org.xhtmlrenderer.util.DownscaleQuality;
import org.xhtmlrenderer.util.FSImageWriter;
import org.xhtmlrenderer.util.ImageUtil;
import org.xhtmlrenderer.util.ScalingOptions;

import java.awt.image.*;

public class Main {

	public static void main(final String[] args) throws Exception{
//		System.out.print(HtmlUtils.htmlEscape("<13>"));
		//Generate an image from a file:
		File file = new File("D:/out/html/1.html");
		Long aid = 1l;
		String imgFx = "png";
		String baseImgPath = "D:/out/html/";
		// maybe it's a file
		if (file.exists()) {
//			Java2DRenderer renderer = new Java2DRenderer(file, 1024);
//			renderer.setBufferedImageType(BufferedImage.TYPE_INT_RGB);
			
			BufferedImage img = Graphics2DRenderer.renderToImageAutoSize(file.toURI().toURL().toExternalForm(), 1024);
			ImageFormat format = ImageFormat.IMAGE_FORMAT_BMP;
	        Map params = new HashMap();

	        // set optional parameters if you like
//	        params.put(SanselanConstants.PARAM_KEY_COMPRESSION, new Integer(TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED));
	        Sanselan.writeImage(img, new File(baseImgPath + aid + ".bmp"), format, params);
//			boolean result = ImageIO.write(img, imgFx, new File(baseImgPath + aid + "." + imgFx));
			
			BufferedImage srcImg = ImageIO.read(new File(baseImgPath + aid + ".bmp"));
//			JPEGImageEncoder   encoder   =   JPEGCodec.createJPEGEncoder(new FileOutputStream(new File(baseImgPath + aid + "_.jpg"))); 
			ImageIO.write(srcImg, "JPEG", new File(baseImgPath + aid + "_.jpg"));
//			PngEncoderB pngEncoderB = new PngEncoderB();
//			pngEncoderB.setImage(srcImg);
//			pngEncoderB.setEncodeAlpha(false);
//			pngEncoderB.setCompressionLevel(9);
//			pngEncoderB.pngEncode();
//			
//			byte[] bytes = pngEncoderB.pngEncode();
//			  FileOutputStream fos=new FileOutputStream(baseImgPath + aid + "_." + imgFx);
//			  fos.write(bytes);
//			  fos.close();
			
			
		}
	}
}
